package com.itcanteen.medical.patient.service.impl;

import com.itcanteen.medical.common.constant.Constant;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.patient.entity.Medication;
import com.itcanteen.medical.patient.mapper.MedicationMapper;
import com.itcanteen.medical.patient.service.MedicationService;
import com.itcanteen.medical.patient.util.SendMsgUtil;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Resource;
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * 提醒 业务层
 * @author 1804A-TeamOne
 * @date 2020-12-17 19:11
 */
@Service
public class MedicationServiceImpl implements MedicationService {

    @Resource
    private MedicationMapper medicationMapper;
    @Resource
    JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 开关提醒
     * @author 1804A-TeamOne
     * @date 2020-12-17 16:22
     * @param medication 药品提醒
     * @param flag 标记
     */
    @Override
    public void onOffRemind(Medication medication, Integer flag) throws AdException {
        if(flag==0){
            medicationMapper.delete(medication);
        }else if(flag == 1){
            medication.setCreateTime(new Date());
            medication.setUpdateTime(new Date());
            medicationMapper.insert(medication);
        }else{
            throw new AdException(Constant.ErrorMsg.REQUEST_PARAM_ERROR);
        }
    }

    /**
     * 根据患者id查找 提醒
     * @author 1804A-TeamOne
     * @date 2020-12-17 19:13
     * @param patientId 患者id
     * @return com.itcanteen.medical.patient.entity.Medication
     */
    @Override
    public Medication findByPatientId(Integer patientId) {
        return medicationMapper.findByPatientId(patientId);
    }

    /**
     * 调用者 定时器上午触发
     */
    @Scheduled(cron = "0 0 8 * * ?")
    public void morning(){
        remindMedication();
    }

    /**
     * 调用者 定时器中午触发
     */
    @Scheduled(cron = "0 0 12 * * ?")
    public void noon(){
        remindMedication();
    }

    /**
     * 调用者 定时器晚上触发
     */
    @Scheduled(cron = "0 0 18 * * ?")
    public void evening(){
        remindMedication();
    }

    /**
     * 将消息存入队列
     * @author 1804A-TeamOne
     * @date 2020-12-17 18:46
     */
    public void remindMedication(){
        Destination destination = new ActiveMQQueue("remind_medication");
        //获取当前开启用药提醒的用户信息
        List<Medication> medications = medicationMapper.selectAll();
        for (Medication medication : medications) {
            Map<String, String> map = new ConcurrentHashMap<>(2);
            map.put("phone",medication.getPatientPhone());
            map.put("content","It’s time to take medicine");
            jmsMessagingTemplate.convertAndSend(destination,map);
        }
    }

    @JmsListener(destination = "remind_medication")
    private void receiveMsg(Map<String,String> map){
        SendMsgUtil.sendMsg(map.get("content"),map.get("phone"));
    }


}
